﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Web;
using System.Linq;
using Share.Services;

namespace Share.Models
{
    public class DBInitializer : DropCreateDatabaseIfModelChanges<DB>
    {
        protected override void Seed(DB db)
        {
            InitCategories(db);
            InitItems(db);
            InitAdvs(db);
            InitCustomers(db);
            InitComments(db);
            InitDirectories(db);
        }

        private void InitDirectories(DB db)
        {
            try
            {
                DS ds = new DS(db, "EZWeb");
                ds.mkdir("First");
                ds.cd("First");
                ds.mkdir("Second");
                ds.cd("/");
                ds.mkdir("Third");
                ds.rmdir("Third",1);
                ds.cd("/First/Second");
                ds.mkdir("Third");

                ds = new DS(db, "EZNexcom");
                ds.mkdir("First");
                ds.cd("First");
                ds.mkdir("Second");
                ds.cd("/");
                ds.mkdir("Third");
                ds.rmdir("Third", 1);
                ds.cd("/First/Second");
                ds.mkdir("Third");
            }
            catch (DbEntityValidationException e)
            {

            }
        }

        private void InitCustomers(DB db)
        {
            Customer c = new Customer();
            c.FirstName = "Frank";
            c.LastName = "Lin";
            c.Email = "frank.lin@gmail.com";
            c.Mobile = "12345678";
            c.Phone = "87654321";
            c.Password = "123";
            c.RegisterAt = DateTime.Now;
            c.State = new State { Name = "Alabama", Code = "AB" };
            c.Country = new Country { Name = "United States", Code = "US" };
            c.Country.States = new List<State>();
            c.Country.States.Add(c.State);
            db.Customers.Add(c);
            db.SaveChanges();
        }

        private void InitComments(DB db)
        {
            List<Item> items = db.Items.ToList();
            Customer customer = db.Customers.Find(1);
            foreach (Item i in items)
            {
                i.Comments = new List<Comment>();
                for (int k = 0; k < 2; k++)
                {
                    Comment c = new Comment { Title = "This primer goes on smooth and light.", Content = "This is AMAZING! Makes my skin feel like velvet & my makeup look flawless! Doesn't accentuate lines & gives my skin a luminous, healthy glow. I've introduced this to several friends & they all love it! Thank you for making such incredible products & taking the guess work out of ingredient lists." };
                    c.Customer = customer;
                    c.CreateAt = DateTime.Now;
                    i.Comments.Add(c);
                }
                db.SaveChanges();
            }
        }

        private void InitAdvs(DB db)
        {
            db.Advs.Add(new Adv { Position = (int)Adv.POSITION.HomeBanner, Photo = "/Content/img/banner1.jpg", Url = "/Product/v2" });
            db.Advs.Add(new Adv { Position = (int)Adv.POSITION.HomePromotion, Photo = "/Content/img/promotion1.jpg", Url = "/Category/Sales" });
            db.Advs.Add(new Adv { Position = (int)Adv.POSITION.HomePromotion, Photo = "/Content/img/promotion2.jpg", Url = "/Category/Sales" });
            db.Advs.Add(new Adv { Position = (int)Adv.POSITION.HomePromotion, Photo = "/Content/img/promotion3.jpg", Url = "/Category/Sales" });
            db.Advs.Add(new Adv { Position = (int)Adv.POSITION.HomePromotion, Photo = "/Content/img/promotion4.jpg", Url = "/Category/Sales" });

            db.SaveChanges();
        }

        private void InitCategories(DB db)
        {
            Category c1, c2;

            db.Categories.Add(c1 = new Category { Name = "Makeup" });
            db.Categories.Add(c2 = new Category { Name = "Face", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Primer", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Concealer", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Tinted Moisturizer", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Foundation", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Powder", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Bronzer", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Luminizer", ParentCategory = c2 });
            db.Categories.Add(c2 = new Category { Name = "Cheeks", ParentCategory = c1 });

            db.Categories.Add(new Category { Name = "Tint", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Powdered Blush", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Pot Rouge", ParentCategory = c2 });
            db.Categories.Add(c2 = new Category { Name = "Lips", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Gloss", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Glaze", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Creamstick", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Butter", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Balm", ParentCategory = c2 });
            db.Categories.Add(c2 = new Category { Name = "Eyes", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Powdered Shadow", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Satin Shadow", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Creamstick", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Liner", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Mascara", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Brow", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Cosmetics Sets", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Brushes", ParentCategory = c1 });

            db.Categories.Add(c1 = new Category { Name = "Skincare" });
            db.Categories.Add(c2 = new Category { Name = "Cleansers", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Toners", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Scrubs & Masks", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Serums", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Moisturizers", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Eye Creams", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Collections", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Brightening", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Resveratrol", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Super Fruits", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Purity", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Mint White Tea", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Jasmine Green Tea", ParentCategory = c2 });

            db.Categories.Add(c1 = new Category { Name = "Bath & Body" });
            db.Categories.Add(c2 = new Category { Name = "Washes", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Scrubs", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Creams", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Serums", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Butters", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Baths", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Soaps", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Flavors", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Blood Orange", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Cabernet Grape", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Cocoa Kona Coffee", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Coconut", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "French Lavender", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Green Apple", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Honey Almond", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Key Lime", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Mangosteen", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Meyer Lemon", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Pink Grapefruit", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Pomegranate", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Strawberry", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Strawberry Lemonade", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "Vanilla Bean", ParentCategory = c2 });
            db.Categories.Add(new Category { Name = "White Peach", ParentCategory = c2 });

            db.Categories.Add(c1 = new Category { Name = "Hands" });
            db.Categories.Add(c2 = new Category { Name = "Hand Creams", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Hand Washes", ParentCategory = c1 });

            db.Categories.Add(c1 = new Category { Name = "Hair" });
            db.Categories.Add(c2 = new Category { Name = "Shampoos", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Conditioners", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Balms", ParentCategory = c1 });

            db.Categories.Add(c1 = new Category { Name = "Kids" });
            db.Categories.Add(c2 = new Category { Name = "Body Washes", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Body Creams", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Bubble Baths", ParentCategory = c1 });
            db.Categories.Add(new Category { Name = "Gift Sets", ParentCategory = c2 });

            db.Categories.Add(c1 = new Category { Name = "Gifts" });
            db.Categories.Add(c2 = new Category { Name = "Weekly Special Value", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Skin Care Gift Sets", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Body Sets", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Gift Sets", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Gift Cards", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "Clutches", ParentCategory = c1 });
            db.Categories.Add(c2 = new Category { Name = "T-Shirts", ParentCategory = c1 });

            db.SaveChanges();
        }

        private void InitItems(DB db)
        {
            string d = "Light reflective pigments diffuse the look of lines and wrinkles and make skin instantly luminous, brighter, healthier and glowing. Infused with red wine resveratrol, vitamins and antioxidants to shield from free radical damage. Silicone-free. ";
            string fd = @"
                <img src='/Content/img/prod5.png' class='pad-10 right'>
                <h4>LUMINOUS PRIMER VITAMINS + ANTIOXIDANTS WITH RESVERATROL</h4>
                <p>100% Natural, 100% Vegan, Gluten free</p>
                <p>
                    Light reflective pigments diffuse the look of lines and wrinkles and make skin instantly
                    luminous, brighter, healthier and glowing. Infused with red wine resveratrol, vitamins
                    and antioxidants to shield from free radical damage. Silicone-free.
                </p>
                <p>
                    <strong>Ingredients: </strong>Organic Aloe Barbadensis Leaf (Aloe Gel)<span class='orgRef'>1</span>,
                    Jojoba Oil, Jojoba Wax Esters, Oryza Sativa (Rice) Starch, Tocopherol (Vitamin E),
                    Sodium Ascorbate (Vitamin C), Euphorbia Cerifera (Candelilla) Wax, Extracts of Organic
                    Camelia Sinensis Leaf Ferment (Green Tea)<span class='orgRef'>2</span> and Resveratrol,
                    Silica, Persea Gratissima Oil (Avocado Butter), Lonicera Caprifolium (Japanese Honeysuckle),
                    Mica
                </p>
                <p>1 certified organic by SGS Organic</p>
                <p>2 certified organic by Oregon Tilth Certified Organic</p>
            ";
            List<Category> cs = db.Categories.ToList();
            foreach (Category c in cs)
            {
                c.Items = new List<Item>();

                for (int i = 0; i < 6; i++)
                {
                    Item item = new Item { No = "123456", Name = "PEACH GLOW", SubName = "*warm peach", Photo = "/Content/img/prod2.jpg", isClearance = true, isHot = true, isNew = true, isSale = true };
                    item.Description = d;
                    item.FullDescription = fd;
                    item.Price = new ItemPrice { Basic = 28, Silver = 26, Gold = 25, Platium = 22, WholeSale = 20 };
                    item.Metric = new ItemProperty { NetCount = 30 };
                    item.Imperial = new ItemProperty { NetCount = 1 };
                    item.CaseMetric = new ItemProperty { NetCount = 30 };
                    item.CaseImperial = new ItemProperty { NetCount = 1 };
                    c.Items.Add(item);
                }
                db.SaveChanges();
            }
        }
    }
}